package org.example.login;

import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

//登录页面UI自动化测试
public class LoginTestUI {
   public static WebDriver webDriver = null;

    @BeforeAll
    public static void getDriver(){
        webDriver = new ChromeDriver();
        webDriver.get("http://123.60.88.108:8080/login.html");
    }

    //导航栏检查
    @Order(1)
    @Test
    public void test1() {
        WebElement element = webDriver.findElement(By.cssSelector("body > div.nav > h1"));
        String text = element.getText();
        if(text.equals("网页聊天")) {
            System.out.println("导航栏测试成功");
        }else {
            System.out.println("导航栏测试失败");
        }
    }

    //登录框检查
    @Order(2)
    @Test
    public void test2() {
        //测试登录框
        WebElement element1 = webDriver.findElement(By.cssSelector("body > div.login-container > div"));
        String e1 = element1.getAttribute("class");
        if(e1.equals("login-dialog")) {
            System.out.println("登录框:测试成功");
        }else {
            System.out.println("登录框测试失败");
        }
    }

    //登录文字提示检查
    @Order(3)
    @Test
    public void test3() {
        WebElement element2 = webDriver.findElement(By.cssSelector("body > div.login-container > div > h3"));
        String text2 = element2.getText();
        if(text2.equals("登录")) {
            System.out.println("登录:测试成功");
        }else {
            System.out.println("登录文字测试失败");
        }
    }

    //用户名文字检查
    @Order(4)
    @Test
    public void test4() {
        WebElement element3 = webDriver.findElement(By.cssSelector("body > div.login-container > div > div:nth-child(2) > span"));
        String text3 = element3.getText();
        if(text3.equals("用户名")){
            System.out.println("用户名文字:测试成功");
        }else {
            System.out.println("用户名文字测试失败");
        }
    }
    //密码文字检查
    @Order(5)
    @Test
    public void test5() {
        WebElement element = webDriver.findElement(By.cssSelector("body > div.login-container > div > div:nth-child(3) > span"));
        String text = element.getText();
        if(text.equals("密码")) {
            System.out.println("密码文字：测试成功");
        }else {
            System.out.println("密码文字测试失败");
        }
    }
    //密码输入框检查
    @Order(6)
    @Test
    public void test6() {
        WebElement element = webDriver.findElement(By.cssSelector("#password"));
        String attribute = element.getAttribute("id");
        if(attribute.equals("password")) {
            System.out.println("密码输入框：测试成功");
        }else {
            System.out.println("密码输入框测试失败");
        }
    }
    //用户名输入框检查
    @Order(7)
    @Test
    public void test7() {
        WebElement element = webDriver.findElement(By.cssSelector("#username"));
        String attribute = element.getAttribute("id");
        if(attribute.equals("username")) {
            System.out.println("用户名输入框：测试成功");
        }else {
            System.out.println("用户名输入框测试失败");
        }
    }

    //登录按钮检查
    @Order(8)
    @Test
    public void test8() {
        WebElement element = webDriver.findElement(By.cssSelector("#submit"));
        String text = element.getText();
        if(text.equals("登录")) {
            System.out.println("登录按钮：测试成功");
        }else {
            System.out.println("登录按钮测试失败");
        }
    }

    //注册按钮检查
    @Order(9)
    @Test
    public void test9() {
        WebElement element = webDriver.findElement(By.cssSelector("#signIn"));
        String text = element.getText();
        if(text.equals("注册")) {
            System.out.println("注册按钮：测试成功");
        }else {
            System.out.println("注册按钮：测试失败");
        }
    }

    //弹窗检查
    @Order(10)
    @Test
    public void test10() {
        WebElement element1 = webDriver.findElement(By.cssSelector("#username"));
        WebElement element2 = webDriver.findElement(By.cssSelector("#password"));
        //输入错误账号和密码
        element1.sendKeys("zhangsan");
        element2.sendKeys("3465345");
        Alert alert = webDriver.switchTo().alert();
        if(alert.getText().equals("登录失败！！！")) {
            System.out.println("弹框测试成功");
            alert.accept();
        }else {
            System.out.println("弹窗测试失败");
        }

    }
}
